package cn.lger.controller;

import cn.lger.pojo.Article;
import cn.lger.pojo.Category;
import cn.lger.pojo.Comment;
import cn.lger.pojo.Page;
import cn.lger.service.ArticleService;
import cn.lger.service.CategoryService;
import cn.lger.service.CommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

/**
 * Code that Changed the World
 *
 * @author Pro
 * @date 2018-06-14.
 */
@RestController
@RequestMapping("api/article")
@Api(tags = "影评文章控制器")
public class ArticleController {

    @Resource
    private ArticleService articleService;
    @Resource
    private CommentService commentService;
    @Resource
    private CategoryService categoryService;

    @GetMapping("list")
    @ApiOperation(httpMethod = "GET", value = "查询所有文章")
    @ApiResponse(code = 200, message = "成功", response = Page.class)
    public Page<Article> getArticles(@ApiParam("当前页，默认0") Integer currentPage,
                                     @ApiParam("一页大小，默认3") Integer size,
                                     @ApiParam("种类，可为null") Integer categoryId){
        return articleService.findOnePage(currentPage, size, categoryId, null);
    }

    @GetMapping("{id}")
    @ApiOperation(httpMethod = "GET", value = "查询一个文章")
    @ApiResponse(code = 200, message = "成功", response = Article.class)
    public Article getArticles(@ApiIgnore @PathVariable int id){
        return articleService.findOneById(id);
    }

    @GetMapping("{id}/comment")
    @ApiOperation(httpMethod = "GET", value = "获取相应id文章的一页评论")
    @ApiResponse(code = 200, message = "成功", response = Page.class)
    public Page<Comment> getComments(
            @ApiIgnore @PathVariable int id,
            @ApiParam("当前页，默认0") Integer currentPage,
            @ApiParam("一页大小，默认3") Integer size){
        return commentService.findOnePageByArticleId(id, currentPage, size);
    }

    @GetMapping("category/list")
    @ApiOperation(httpMethod = "GET", value = "获取所有文章分类")
    @ApiResponse(code = 200, message = "成功", response = List.class)
    public List<Category> getCategories(){
        return categoryService.findAll();
    }



}
